<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class app_login_auth extends Model {

	public function CheckUserDB($username, $password)
	{
		$resulta = $this->db->query("SELECT A.USERNAME, A.AKTIF, C.KD_AKSES, B.NAMA, b.ID, case when D.ALLOW is null then false else true end as ALLOW
		FROM zusers A
		INNER JOIN ( 	SELECT NIM AS ID, NAMA_MAHASISWA AS NAMA FROM m_mahasiswa
		UNION ALL
		SELECT NIP AS ID, NAMA_PEGAWAI AS NAMA FROM M_PEGAWAI ) B ON A.ID = B.ID
		INNER JOIN ZAKSES_USER C ON A.KD_USER = C.KD_USER
		LEFT JOIN TRUSTEE_DOWNLOAD D ON A.KD_USER = D.KD_USER
		WHERE A.USERNAME = '" .$username. "' AND A.PASSWORD = '" .MD5(MD5(MD5($password))). "' AND A.AKTIF = '1'");
		$akses = '';
		$id;
		$name;
		$allow;
		if ( $resulta->num_rows() == 1)
		{
			foreach($resulta->result() as $h)
			{	
				$akses = $h->KD_AKSES ;
				$id = $h->ID ;
				$name = $h->NAMA ;
				$allow = $h->ALLOW ;
			}
			$data = array('username' => $username, 
						'login' => TRUE,
						'akses' => $akses,
						'ID' => $id,
						'name' => $name,
						'down' => $allow);
			$this->session->set_userdata($data);
			return $akses;
		}
		else
		{
			return $akses;
		}
		
	}

}